package com.y2.authprj.mvc.controller.auth;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.jws.WebParam.Mode;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.gdglc.spring.biz.auth.StaffInfoBiz;
import com.y2.authprj.bean.auth.model.StaffInfo;
import com.y2.authprj.bean.auth.model.SysroleInfo;
import com.y2.authprj.bean.auth.vo.StaffInfoVo;
import com.y2.authprj.exception.BizException;


@Controller
@RequestMapping("/staff")
public class StaffController {

	@Resource(name="StaffInfoBiz")
	private StaffInfoBiz biz;
	public StaffInfoBiz getBiz() {
		return biz;
	}
	public void setBiz(StaffInfoBiz biz) {
		this.biz = biz;
	}
	
	public StaffController(){
		System.out.println("create staffController");
		
	}
	
	@RequestMapping("/list")
	public String show(StaffInfoVo vo,Model model){
		try {
			List<StaffInfoVo> staffs = biz.findAllVo(vo);
			model.addAttribute("staffs", staffs);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "user/list";
	}
	

	
	@RequestMapping("/add")
	public String add(StaffInfo staff,Model model,RedirectAttributes response){
		if(biz.findStaffNum(staff.getStaffNum())==0){
			try {
				staff.setCreateTime(new Date());
				staff.setLoginTime(new Date());
				int i = biz.addSta(staff);
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
			
			
	
		
		return "redirect:/staff/list";
	}
	
	@RequestMapping("/showUpdateInfo")
	@ResponseBody
	public StaffInfo showUpdate(@RequestParam("id")String id) throws NumberFormatException, Exception{
		StaffInfo staffInfo = biz.StaffInfoById(Integer.parseInt(id));
		return staffInfo;
	}
	
	@RequestMapping("/updateStaff")
	public String update(StaffInfo staff,Model model){
		try {
			
			biz.updateStaff(staff);
			System.out.println("OK");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/staff/list";
	}
	
	@RequestMapping("/del")
	public String deleteStaById(@RequestParam(value="dat",required=false) String[] datas){
		
		if(datas!=null){
			for(int i=0;i<datas.length;i++){
				try {
					int count=biz.deleteStaById(Integer.parseInt(datas[i].toString()));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		
		return null;
	}
	

	@RequestMapping("/isExitsNum")
	@ResponseBody
	public String isExitsNum(String staffNum,Model model){
		int i=0;
		try {
			i=biz.findStaffNum(staffNum);
			if(i>0){
				return "false";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "true";
		
	}
	

	
	
}
